package me.panpf.sketch.process;


import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.request.Resize;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.Locale;

/**
 * 在图片上面个盖上一层颜色，可兼容形状不规则的透明图片
 */
public class MaskImageProcessor extends WrappedImageProcessor {

    private Paint paint;
    private int maskColor;

    /**
     * 创建一个遮罩图片处理器
     *
     * @param maskColor 遮罩颜色
     * @param wrappedProcessor 嵌套一个图片处理器
     */
    public MaskImageProcessor(int maskColor, WrappedImageProcessor wrappedProcessor) {
        super(wrappedProcessor);
        this.maskColor = maskColor;
    }

    /**
     * 创建一个遮罩图片处理器
     *
     * @param maskColor 遮罩颜色
     */
    public MaskImageProcessor(int maskColor) {
        this(maskColor, null);
    }


    @Override
    public PixmapBuild onProcess(Sketch sketch, PixmapBuild bitmap, Resize resize, boolean lowQualityImage) {
        if (bitmap.getPixelMap().isReleased()) {
            return bitmap;
        }

        BitmapPool bitmapPool = sketch.getConfiguration().getBitmapPool();

//        Bitmap.Config newBitmapConfig = bitmap.getConfig();
        PixelFormat newBitmapConfig = bitmap.getPixelMap().getImageInfo().pixelFormat;

        if (newBitmapConfig == null) {
            newBitmapConfig = lowQualityImage ? PixelFormat.UNKNOWN : PixelFormat.ARGB_8888;
        }

        PixmapBuild maskBitmap;
        boolean isNewBitmap = false;
        if (bitmap.getPixelMap().isEditable()) {
            maskBitmap = bitmap;
        } else {
            maskBitmap = bitmapPool.getOrMake(bitmap.getPixelMap().getImageInfo().size.width, bitmap.getPixelMap().getImageInfo().size.height, newBitmapConfig);
            isNewBitmap = true;
        }

        Canvas canvas = new Canvas(new Texture(maskBitmap.getPixelMap()));

        if (isNewBitmap) {
//            canvas.drawBitmap(bitmap, 0, 0, null);
            canvas.drawPixelMapHolder(new PixelMapHolder(bitmap.getPixelMap()), 0, 0, new Paint());
        }

        if (paint == null) {
            paint = new Paint();
            paint.setColor(new Color(maskColor));
        }
//        paint.setXfermode(null);
        paint.setBlendMode(null);
//        RectFloat bounds, Paint paint
        RectFloat bounds = new RectFloat();
        bounds.right = bitmap.getPixelMap().getImageInfo().size.width;
        bounds.bottom = bitmap.getPixelMap().getImageInfo().size.height;
        int src = canvas.saveLayer(bounds, paint);
//        int src = canvas.saveLayer(0, 0, bitmap.getImageInfo().size.width, bitmap.getImageInfo().size.height, paint, Canvas.ALL_SAVE_FLAG);

//        canvas.drawBitmap(bitmap, 0, 0, null);
        canvas.drawPixelMapHolder(new PixelMapHolder(bitmap.getPixelMap()), 0, 0, paint);

//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setBlendMode(BlendMode.SRC_IN);
//        canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);
        canvas.drawRect(bounds, paint);

        canvas.restoreToCount(src);

        return maskBitmap;
    }


    /**
     * 获取遮罩颜色
     * @return int
     */
    public int getMaskColor() {
        return maskColor;
    }

    @Override
    public String onToString() {
        return String.format(Locale.US, "%s(%d)", "MaskImageProcessor", maskColor);
    }

    @Override
    public String onGetKey() {
        return String.format(Locale.US, "%s(%d)", "Mask", maskColor);
    }
}
